//
//  AddPeopleHelpViewController.m
//  AllFamily
//
//  Created by chirui on 2018/5/29.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "AddPeopleHelpViewController.h"

@interface AddPeopleHelpViewController (){
    
    NSData * PhtotData;
}
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *nameText;
@property (weak, nonatomic) IBOutlet UITextField *cardNumberText;
@property (weak, nonatomic) IBOutlet UITextField *relationText;
@property (weak, nonatomic) IBOutlet UIButton *photoBtn;

@end

@implementation AddPeopleHelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    TitleView(@"添加人员");
    
    self.titleLabel.numberOfLines = 3;
    [self.titleLabel sizeToFit];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)photoBtnClick:(UIButton *)sender {
    
    if([[UIResponder currentFirstResponder] isKindOfClass:[UITextField class]]) {
        [[UIResponder currentFirstResponder] resignFirstResponder];
    }
    [BDImagePicker showImagePickerFromViewController:self allowsEditing:YES finishAction:^(UIImage *image) {
        if (image) {
         
              PhtotData  = UIImageJPEGRepresentation(image, 0.5);
            [_photoBtn setBackgroundImage:image forState:UIControlStateNormal];
        }
    }];
}

- (IBAction)surebtnClick:(UIButton *)sender {
    
    if (_nameText.text.length<=0) {
        SHuiMBhudTextTimeStr(@"请输入姓名");
        return;
    }
    if (_cardNumberText.text.length<=0) {
        SHuiMBhudTextTimeStr(@"请输入身份证号");
        return;
    }
    if (_relationText.text.length<=0) {
        SHuiMBhudTextTimeStr(@"请输入双方关系");
        return;
    }
    
    if (PhtotData==nil) {
        SHuiMBhudTextTimeStr(@"请上传户口对应照片");
        return;
    }
    
    [self upDataRequest];
}

-(void)upDataRequest{
    
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"name":_nameText.text,
                           @"id_card_num":_cardNumberText.text,
                           @"relation":_relationText.text
                           };

    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    SHuiMBhubNoTimeStr(@"正在提交...");
    [manager POST:RequestUrlStr(add_peopleHelp_api) parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData){
     
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
            [formData appendPartWithFileData:PhtotData name:@"image" fileName:fileName mimeType:@"image/png"];
      
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        //打印下上传进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]==200) {
            
            SHuiMBhudTextTimeStr(@"提交成功");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    } ];
    
}



@end
